package com.jumi.microservice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.common.constant.ResultEnum;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.IResponseRxceptionCode;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.RelationTemplateDO;
import com.jumi.microservice.domain.RelationTemplateDTO;
import com.jumi.microservice.mapper.RelationTemplateMapper;
import com.jumi.microservice.service.RelationTemplateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 关联版式表 服务实现类
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
@Service
@Transactional
public class RelationTemplateServiceImpl extends ServiceImpl<RelationTemplateMapper, RelationTemplateDO> implements RelationTemplateService {

    /**
     * 新增关联版式信息
     * @param dto
     */
    @Override
    public void saveRelationTemplate(RelationTemplateDTO dto) {
        RelationTemplateDO po = dto2Po(dto);
        //判断数据转换是或为空
        ExceptionEnum.FAIL_RELATIONTRMP_NULL.doThrowIf(po==null);
        LocalDateTime now = LocalDateTime.now();
        po.setCreateTime(now);
        po.setUpdateTime(now);
        baseMapper.insert(po);
    }

    /**
     * 删除关联版式
     * @param ids 主键id
     * @param uname 操作人
     */
    @Override
    public void delete(String ids, String uname) {
        //判断id是否为空
        ExceptionEnum.FAIL_RELATIONTRMP_NULL.doThrowIf(StringUtils.isEmpty(ids));
        List<RelationTemplateDO> dos = baseMapper.selectList(
                Wrappers.<RelationTemplateDO>lambdaQuery()
                        .in(RelationTemplateDO::getId,ids.split(",")));
        ExceptionEnum.FAIL_RELATIONTRMP_NULL.doThrowIf(dos.size()== NumberConstant.NUMBER_ZERO);
        dos.stream().forEach(d->{
            ExceptionEnum.FAIL_RELATIONTRMP_NULL.doThrowIf(d==null);
            //启动状态抛出信息
            if(StatusEnum.STATUS_YES.getCode()==d.getStatus()) {
                throw new BaseException(new IResponseRxceptionCode(ResultEnum.FAIL.getCode(), StrUtil.format(ExceptionEnum.FAIL_RELATION_STATUS.getMessage(), d.getId())));
            }
            d.setIsDel(StatusEnum.STATUS_YES.getCode());
            d.setUpdateTime(LocalDateTime.now());
            d.setModifier(uname);
            baseMapper.updateById(d);
        });
    }

    /**
     * 修改关联版式
     * @param dto
     */
    @Override
    public void updateRt(RelationTemplateDTO dto) {
        RelationTemplateDO po = dto2Po(dto);
        ExceptionEnum.FAIL_RELATIONTRMP_NULL.doThrowIf(po==null);
        po.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(po);
    }

    @Override
    public RelationTemplateDTO selectById(Long id) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        RelationTemplateDO po = baseMapper.selectById(id);
        ExceptionEnum.FAIL_RELATIONTRMP_NULL.doThrowIf(po==null);
        return po2Dto(po);
    }

    /**
     * 启用/禁用关联版式
     * @param id 主键id
     * @param status 状态，1启用，0.禁用
     * @param uname 用户名
     */
    @Override
    public void enableStatus(Long id, Integer status, String uname) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        RelationTemplateDO po = baseMapper.selectById(id);
        ExceptionEnum.FAIL_RELATIONTRMP_NULL.doThrowIf(po==null);
        po.setStatus(status);
        po.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(po);
    }

    /**
     * dto->po
     * @param dto
     * @return
     */
    private RelationTemplateDO dto2Po(RelationTemplateDTO dto){
        RelationTemplateDO po = null;
        try{
            po = dto.clone(RelationTemplateDO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return po;
    }

    /**
     *
     * @param po
     * @return
     */
    private RelationTemplateDTO po2Dto(RelationTemplateDO po){
        RelationTemplateDTO dto = null;
        try{
            dto = po.clone(RelationTemplateDTO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return dto;
    }
}
